package com.service.sys;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.entity.sys.SysUser;
import com.entity.sys.query.SysQuery;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mapper.sys.SysUserMapper;
import com.common.base.BaseService;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class SysUserService extends BaseService<SysUserMapper, SysUser> {

    /**
     * 根据用户名获取用户
     */
    public SysUser getUserByName(String userName){
        SysQuery queryVo = new SysQuery();
        queryVo.setName(userName);
        return baseMapper.getById(sysParams(queryVo));
    }

    /**
     * 根据用户id获取用户
     */
    public SysUser getUserById(Long id){
        SysQuery queryVo = new SysQuery();
        queryVo.setId(id);
        return baseMapper.getById(sysParams(queryVo));
    }

    /**
     * 用户列表分页
     */
    public PageInfo<SysUser> page(SysQuery queryVo) {
        PageHelper.startPage(queryVo.getPageNum(),queryVo.getPageSize());
        List<SysUser> list = baseMapper.page(sysParams(queryVo));
        return new PageInfo<>(list);
    }

    /**
     * 校验用户名和手机号码是否唯一
     */
    public boolean checkUserNameAndPhone(String userName,String phoneNumber,String id) {
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper();
        if (StrUtil.isNotBlank(userName)){
            queryWrapper.eq("user_name",userName);
        }
        if (StrUtil.isNotBlank(phoneNumber)){
            queryWrapper.eq("phone",phoneNumber);
        }
        if (StrUtil.isNotBlank(id)){
            queryWrapper.ne("id",id);
        }
        int count = baseMapper.selectCount(queryWrapper);
        return count > 0 ? true : false;
    }
}
